package cn.wgx.activiti.demo.web;

import cn.wgx.activiti.demo.entity.BlankObject;
import com.github.pagehelper.Page;
import java.util.*;

public class BaseController {

    private final static List emptyList = new ArrayList<>();
    private final static BlankObject blankObject = new BlankObject();





    protected Map<String,Object> getInitMap(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
//        map.put("count", 0);
        map.put("data", blankObject);
        map.put("success", true);
        return map;
    }

    public Map<String,Object> returnData(Object o){
        return returnData(o, null, null,null);
    }

    public Map<String,Object> returnData(Object o, String key){
        return returnData(o, null, null,key);
    }

    public Map<String,Object> returnData(Object o, Map map){
        return returnData(o, null, map,null);
    }

    public Map<String,Object> returnData(Object o , Page page){
        return returnData(o, page, null,null);
    }

    public Map<String,Object> returnData(Object o , Page page, Map map, String key){
        if(map == null) {
           map = getInitMap();
        }
        if(null == o){
            o = blankObject;
        }
        if(key == null){
            map.put("data", o);
        }else{
            map.put(key, o);
        }
        if(page != null){
            map.put("count", page.getTotal());
            map.put("pages", page.getPages());
            map.put("pageNum", page.getPageNum());
        }
        return map;
    }

    protected Map<String,Object> returnErrorMap(String errorMsg){
        return returnErrorMap(errorMsg, null, null);
    }
    protected Map<String,Object> returnErrorMap(String errorMsg, Integer code){
        return returnErrorMap(errorMsg, code, null);
    }
    protected Map<String,Object> returnErrorMap(String errorMsg, Map<String,Object> map){
        return returnErrorMap(errorMsg,null, map);
    }

    protected Map<String,Object> returnErrorMap(String errorMsg, Integer code, Map<String,Object> map){
        if(map == null) {
            map = getInitMap();
        }
        map.put("msg", errorMsg == null ? "error" : errorMsg);
        map.put("code", code == null ? 0 : code);
        map.put("status", -1);
        map.put("success", false);
        return map;
    }
}
